﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Model;
using AspNetCs.Bll;
using System.Data;
using System.Collections;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.memorial_enum;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Common;
using AspNetCs.Enumerations.Channel_Enum;

public partial class Users_AncestralHallSkins : BasePage
{
    protected int AncestralHallId = 0;
    protected AncestralHallBLL ancBLL = AncestralHall_Bll.CreatNew;
    private AncestralHallItemsBLL bll = new AncestralHallItemsBLL();
    protected AspNetCs.Bll.Temple templateBLL = AspNetCs.Bll.TempleBll.CreateNew;
    protected DataTable dataTable = new DataTable();

    protected int TempId1 = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckUserExit("Login.Aspx", "请您登录", true);
        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Request["id"]))
            {
                ResultStr("Login.aspx", "参数错误");
            }
            else
            {
                ViewState["Id"] = Request["id"];
                this.AncestralHallId = StringHelper.isNumber_int(Request["id"]);
                dataTable = ancBLL.GetAncestralHallInfoById(this.AncestralHallId);

                if (dataTable.Rows.Count > 0)
                {
                    TempId1 = StringHelper.isNumber_int(dataTable.Rows[0]["TempleId"].ToString());
                }
            }
        }
        else
        {
            if (ViewState["Id"] != null)
            {
                this.AncestralHallId = StringHelper.isNumber_int(ViewState["Id"].ToString());
            }
        }

        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/AncestralHallSkins_" + AncestralHallId + ".Aspx", "请您登录", true);

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        DrePoint();
    }

    protected void DrePoint()
    {

        int ClanTempleId = StringHelper.isNumber_int(ViewState["Id"]);
        //AncestralHall_Model ClanMd = AncestralHall_Bll.CreatNew.GetModel(ClanTempleId);

        Users userBll = Users_Bll.CreatNew;
        int UserId = Base_GetUserInfo.UserId;
        //int needPoint = CreateNetMuPoint(NetMuTypeEnum(model.MuType));
        decimal TemplatePoint = StringHelper.isNumber_decimal(Request["hldTemplePoint"]);
        int TempId = StringHelper.isNumber_int(Request["templateId"]);
        if (TempId == TempId1)
        {
            Response.Write("<script>alert('保存成功!');location.href='AncestralHallSkins_" + ViewState["Id"] + ".Aspx';</script>");
            return;
        }
        if (UserId != 0)
        {
            if (TemplatePoint != 0)
            {
                if (TemplatePoint <= (int)Base_GetUserInfo.Point)
                {

                    Temple_Model TmpMd = templateBLL.GetModel(TempId);
                    if (TmpMd.Id > 0)
                    {

                        Point_Log_Model PointMd = new Point_Log_Model();
                        PointMd.Addtime = DateTime.Now;
                        PointMd.NetIp = StringHelper.Ip();
                        PointMd.Point = TemplatePoint;
                        PointMd.Readme = "用户修改" + EnumDescription.GetFieldText(Channel_Type.clan) + " 模板为" + TmpMd.TempleName + "，扣除点数" + TemplatePoint + "点";
                        PointMd.UserId = UserId;
                        Point_Log_Bll.CreateNew.Add(PointMd);
                    }
                    else
                    {
                        Response.Write("<script>alert('保存失败,当前模板不存在!');location.href='AncestralHallSkins_" + ViewState["Id"] + ".Aspx';</script>");
                        return;
                    }
                }
                else
                {
                    Response.Write("<script>alert('保存失败,用户点数不足!');location.href='AncestralHallSkins_" + ViewState["Id"] + ".Aspx';</script>");
                    return;
                }


            }
            AncestralHall_Bll.CreatNew.filterUpdate(" TemplateId= " + TempId, " id=" + ViewState["Id"]);
            //NetMu_Bll.CreatNew.filterUpdate(" TemplateId= " + TempId, " id=" + ViewState["Id"]);
        }
        else
        {
            CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/AncestralHallSkins_" + ViewState["Id"] + ".Aspx", "请您登录", true);
        }
        Response.Write("<script>alert('保存成功!');location.href='UpdateHappyPet_" + ViewState["Id"] + ".Aspx';</script>");

    }


}
